package com.jingan.nursinghome.regulatory.dialog2;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.model.Categirues2;
import com.jingan.nursinghome.regulatory.model.Topic;
import com.jingan.nursinghome.regulatory.view.CircleView;
import com.jingan.nursinghome.regulatory.view.MyActionBar;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/25.
 */

public class XuantiMenuDialog3Little extends BaseDialog {

    public XuantiMenuDialog3Little(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_richang2_xuanti2, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        initUI();
        adapterUI();
    }

    public static XuantiMenuDialog3Little xuantiDialog;
    static String mTitile;

    public static List<Categirues2> mCategirues2s = new ArrayList<>();

    public static XuantiMenuDialog3Little getInstance(BaseActivity baseActivity, String titile, List<Categirues2> categirues2s) {
        mTitile = titile;
        mCategirues2s = categirues2s;
        if (isFinishing(xuantiDialog)) {
            xuantiDialog = new XuantiMenuDialog3Little(baseActivity);
        }
        return xuantiDialog;
    }

    MyActionBar myActionBar;
    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    TextView appraise;

    private void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "选题");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                if (itemPosition == 0) {
                    outRect.top = ProportionUtils.getInstance().adapterInt(25);
                }
            }
        });

        appraise = (TextView) findViewById(R.id.appraise);
        appraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //选题完成

                RichangDialog2.richangDialog2.listener();//数据更新

                dismiss();
                XuantiMenuDialog.xuantiDialog.dismiss();
            }
        });

    }

    class MyAdapter extends RecyclerView.Adapter<XuantiMenuDialog3Little.MyAdapter.MyViewHoldView> {

        public MyAdapter() {
        }

        class MyViewHoldView extends RecyclerView.ViewHolder {
            TextView info;
            CircleView mycircle;

            public MyViewHoldView(View itemView) {
                super(itemView);
                ProportionUtils.getInstance().adapterView(itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout));
                info = (TextView) itemView.findViewById(R.id.info);
                mycircle = (CircleView) itemView.findViewById(R.id.mycircle);
                ProportionUtils.getInstance().adapterTextView(info);
                ProportionUtils.getInstance().adapterView(itemView.findViewById(R.id.line));
            }
        }

        @Override
        public MyViewHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_xuanti3, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
            return new MyViewHoldView(view);
        }

        @Override
        public void onBindViewHolder(final MyViewHoldView holder, final int position) {
            holder.info.setTextColor(Color.parseColor("#444444"));
            holder.mycircle.setVisibility(View.INVISIBLE);
            final String s = mCategirues2s.get(position).getName();
            holder.info.setText(s);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击事件
                    List<Topic> topicList = MicrometerTable.getInstance().getCategoryTopics(mTitile, CreateMicrometerTableDailog2.rid);
                    List<Topic> topicList2 = new ArrayList<Topic>();
                    for (int i = 0; i < topicList.size(); i++) {
                        if (topicList.get(i).getTitleID().equals(mCategirues2s.get(position).getId())) {
                            topicList2.add(topicList.get(i));
                        }
                    }
                    String t=s.substring(s.indexOf("、")+1,s.indexOf("---"));
                    XuantiMenuDialog2.getInstance(baseActivity, t, topicList2);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mCategirues2s.size();
        }
    }

    private void adapterUI() {
//        ProportionUtils.getInstance().adapterView(findViewById(R.id.dialog_richang2_constraintLayout));
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_richang2_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(appraise);
    }

    @Override
    protected void listener() {
        String t=mTitile.substring(mTitile.indexOf("、")+1);
        myActionBar.setLeftBimapAndTitle(null, "选题-" + t);
        recyclerView.setAdapter(new MyAdapter());
    }

    @Override
    protected void recycleView() {

    }
}
